package own.stu.jobgib.playown.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * park & unPark是以线程为单位来阻塞和唤醒线程，
 * 可以先执行unPark
 * interrupted 会使 park 失效
 */
public class LockSupportTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
                LockSupport.park();
                System.out.println(" -------- 1 ------");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });
        t1.start();
        LockSupport.unpark(t1);
        System.out.println(" ---- main unpark first -----");
    }

    public static void main2(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println(" -------- 1 ------");
            LockSupport.park();
            System.out.println(" -------- 2.1 ------ " + Thread.interrupted());
            System.out.println(" -------- 2.2 ------ " + Thread.interrupted());
            LockSupport.park();
            System.out.println(" -------- 3 ------");
        });
        t1.start();

        TimeUnit.MILLISECONDS.sleep(200);
        LockSupport.unpark(t1);
        LockSupport.unpark(t1);
        TimeUnit.MILLISECONDS.sleep(50);
        t1.interrupt();

        TimeUnit.MILLISECONDS.sleep(200);
        System.out.println(" --------after main interrupt------");
        LockSupport.unpark(t1);
    }
}
